#include <glf.h>
#include <glfwindow.h>
#include <glfevent.h>

static void think(void* userdata)
{
	glfEvent e;
    int* hasinit = userdata;
    if(!*hasinit){
        glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
		//glfSetCursorFlags( glfCursorLock );
        *hasinit = 1;
    }
	while( glfPopEvent( &e ) )
	{
		switch( e.type )
		{
		case glfEventKeyUp:
		case glfEventKeyDown:
			printf( "%u %c %u\n", e.type, e.key.scancode, e.key.scancode );
			break;
		case glfEventMouse:
			printf( "%d,%d\t%d,%d\n", e.mouse.x, e.mouse.y, e.mouse.dx, e.mouse.dy );
			break;
		default:
			break;
		}
	}
}

static void render(void* userdata)
{
    /* swapping buffers is done by the mainloop implicitly */
    glClear(GL_COLOR_BUFFER_BIT);
}

static void resize(void* userdata)
{
   glViewport(0,0,glfGetWidth(),glfGetHeight());
}

int main(void)
{
    int hasinit = 0;

    glfConfigSetAttribute(glfMajorVersion, 3);
    glfConfigSetAttribute(glfMinorVersion, 0);
    glfConfigSetAttribute(glfMultiSampleLevel, 1);
    glfConfigSetAttribute(glfContextFlags, glfContextForwardCompatible);
	glfConfigSetAttribute(glfDisableVsync, 1 );
    
    if( !glfCreateWindow( "GLF Empty Example", 1024, 768 ) )
        return -1;
    glfSetCallBack(glfCallbackThink, think, &hasinit);
    glfSetCallBack(glfCallbackRender, render, NULL);
    glfSetCallBack(glfCallbackResize, resize, NULL);
    
    if( !glfMainloop() )
        return -1;
    glfDestroyWindow();
    return 0;
}
